/**
 * @author gc291
 */
function errorText(text) {
	return '<p class="ui-state-error"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-alert"></span><strong>Alert:</strong>' + text + '</p>';
}

function highlightText(text) {
	return '<p class=\"ui-state-error\"><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 0.3em;\"></span><strong>Alert:</strong>' + text +'<span class=\"ui-icon ui-icon-close\" style=\"float: right; margin-right: 0.3em;\"/></p>';
}

var TIS = TIS ||
{};

(function($){
	
	$(document).ready(function(){
			
		$("#checkRegistration").bind("click", function(event){
			
			event.preventDefault();
			
			 $.ajax({
	         	type: "GET",
	            url: "student.php",
	            data: ({
	            	checkRegistration:''
	            }),
	            success: function(response){
					$('#messages').html(response);
					$('.ui-state-highlight').effect("highlight", {}, 3000);		
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$('#messages').html(errorText(textStatus) + XMLHttpRequest.responseText);
					$('.ui-state-error').effect("highlight", {}, 3000);
				}
			});
		});
	
		$("button#changeTripos").bind("click", function(event){
			
			event.preventDefault();
			
			$.ajax({
	         	type: "GET",
	            url: "student.php",
	            data: ({
	            	changeTripos:''
	            }),
	            success: function(response){
					$('#tripos').html(response);
					
					$("#messages").html('<p class="ui-state-highlight ui-corner-all"><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: 0.3em;\"/><strong>Attention:</strong>By changing your tripos you will lose any saved choices!</p>');
					$('.ui-state-highlight').effect("highlight", {}, 3000);
				},
				error: function(){
					$('#messages').html(errorText("We encountered an error"));
					$('.ui-state-error').effect("highlight", {}, 3000);
					
				}
			});
		});
		
	    $("form#choice input.choice").bind("click", function(event){
			event.preventDefault();
			
			var input = $(this);
	
	    	// If the checkbox is unchecked
	        if (input.is(':checked')) {
				var myObj = $(this);
	            $.ajax({
	                type: "POST",
	                url: "student.php",
	                data: ({
	                    addchoice: input.attr('value')
	                }),
	                success: function(response){
						input[0].checked = true;
						$('#messages').html(response);
						input.parent().css("background-color:green;");
						$('.ui-state-highlight').effect("highlight", {}, 3000);
						$('.ui-state-highlight').hide("drop", { direction: "up" }, 1000);
	                },
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('#messages').html(errorText(textStatus) + XMLHttpRequest.responseText);
						$('.ui-state-error').effect("highlight", {}, 3000);
					}
						
	            });
	
	        // If the checkbox is already checked
	        } else {
	            
	            $.ajax({
	                type: "POST",
	                url: "student.php",
	                data: ({
	                    removechoice: input.attr('value')
	                }),
	                success: function(response){
						input[0].checked = false;
						$('#messages').html(response);
						$('.ui-state-highlight').effect("highlight", {}, 3000);
						$('.ui-state-highlight').hide("drop", { direction: "up" }, 1000);
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('#messages').html(errorText(textStatus) + XMLHttpRequest.responseText);
						$('.ui-state-error').effect("highlight", {}, 3000);
					}
	            });
	        };
			
		});
	});
}(jQuery));